package com.lxmes.device.service.impl;

import com.lxmes.common.exception.ServiceException;
import com.lxmes.common.utils.DateUtils;
import com.lxmes.common.utils.StringUtils;
import com.lxmes.common.utils.bean.BeanValidators;
import com.lxmes.device.domain.NcEmJggx;
import com.lxmes.device.mapper.NcEmJggxMapper;
import com.lxmes.device.service.INcEmJggxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Validator;
import java.util.List;

/**
 * 加工程序存储Service业务层处理
 *
 * @author ruoyi
 * @date 2023-02-10
 */
@Service
public class NcEmJggxServiceImpl implements INcEmJggxService
{
    @Autowired
    private NcEmJggxMapper ncEmJggxMapper;

    @Autowired
    protected Validator validator;

    /**
     * 查询加工程序存储
     *
     * @param id 加工程序存储主键
     * @return 加工程序存储
     */
    @Override
    public NcEmJggx selectNcEmJggxById(Long id)
    {
        return ncEmJggxMapper.selectNcEmJggxById(id);
    }

    /**
     * 查询加工程序存储列表
     *
     * @param ncEmJggx 加工程序存储
     * @return 加工程序存储
     */
    @Override
    public List<NcEmJggx> selectNcEmJggxList(NcEmJggx ncEmJggx)
    {
        return ncEmJggxMapper.selectNcEmJggxList(ncEmJggx);
    }

    /**
     * 新增加工程序存储
     *
     * @param ncEmJggx 加工程序存储
     * @return 结果
     */
    @Override
    public int insertNcEmJggx(NcEmJggx ncEmJggx)
    {
        ncEmJggx.setCreateTime(DateUtils.getNowDate());
        return ncEmJggxMapper.insertNcEmJggx(ncEmJggx);
    }

    /**
     * 修改加工程序存储
     *
     * @param ncEmJggx 加工程序存储
     * @return 结果
     */
    @Override
    public int updateNcEmJggx(NcEmJggx ncEmJggx)
    {
        ncEmJggx.setUpdateTime(DateUtils.getNowDate());
        return ncEmJggxMapper.updateNcEmJggx(ncEmJggx);
    }

    /**
     * 批量删除加工程序存储
     *
     * @param ids 需要删除的加工程序存储主键
     * @return 结果
     */
    @Override
    public int deleteNcEmJggxByIds(Long[] ids)
    {
        return ncEmJggxMapper.deleteNcEmJggxByIds(ids);
    }

    /**
     * 删除加工程序存储信息
     *
     * @param id 加工程序存储主键
     * @return 结果
     */
    @Override
    public int deleteNcEmJggxById(Long id)
    {
        return ncEmJggxMapper.deleteNcEmJggxById(id);
    }
    @Override
    public String imporIteminfo(List<NcEmJggx> ncEmJggxList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(ncEmJggxList) || ncEmJggxList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (NcEmJggx ncEmJggx : ncEmJggxList)
        {
            try
            {
                BeanValidators.validateWithException(validator, ncEmJggx);
                ncEmJggx.setCreateBy(operName);
                ncEmJggx.setUpdateBy(operName);
                this.insertNcEmJggx(ncEmJggx);
                successNum++;
                successMsg.append("<br/>" + successNum + "数据导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/> 数据导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
